//
//  ByHomeVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByOrganizationVC.h"

/** 视图 */
#import "ByCoreViewCarousel.h"          // 轮播视图

/** 单元格 */
#import "ByNotificationCell.h"          // 通知cell
#import "ByTitleCell.h"                 // 标题cell
#import "ByContentCell.h"               // 内容cell

/** api */
#import "ByNetNewsApi.h"

@interface ByOrganizationVC () <UITableViewDataSource, UITableViewDelegate>
{
    NSInteger _page;        // 数据页码
}
@property (nonatomic, strong) UITableView *table;
//@property (nonatomic, strong) ByCoreViewCarousel *carouselView;

@property (nonatomic, strong) NSMutableArray *items;
@property (nonatomic, strong) NSMutableArray *normaelItems;
@property (nonatomic, strong) NSMutableArray *viceItems;

@end

@implementation ByOrganizationVC

#pragma mark - getter
- (NSMutableArray *)items {
    if (!_items) {
        _items = [[NSMutableArray alloc] init];
    }
    return _items;
}
- (NSMutableArray *)normaelItems {
    if (!_normaelItems) {
        _normaelItems = [[NSMutableArray alloc] init];
    }
    return _normaelItems;
}
- (NSMutableArray *)viceItems {
    if (!_viceItems) {
        _viceItems = [[NSMutableArray alloc] init];
    }
    return _viceItems;
}

//- (ByCoreViewCarousel *)carouselView {
//    if (!_carouselView) {
//        _carouselView = [[ByCoreViewCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2) timerDuration:2];
//    }
//    return _carouselView;
//}


#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
    [self getCarouselImages];

    
    [self.table.mj_header beginRefreshing];

}

#pragma mark - net
- (void)postQuery {
    
    [self getCarouselImages];
    [self postQueryForOrganization];
    [self postQueryForOrganization_normal];
    [self postQueryForOrganization_vice];
}
/** 获取轮播图 */
- (void)getCarouselImages {
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.position = [NSString stringWithFormat:@"%i", 6045];
    params.page_num = @"1";
    params.page_size = @"5";
    
    [ByNetImageApi imageCarouselGet:params success:^(ByNetBaseResult *result) {
        /** 设置轮播 */
        self.carouselView.models = result.data;
    } failure:^(ByNetBaseResult *result) {
        
    }];
}
/** 理事会 */
- (void)postQueryForOrganization {
    
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_ORGANIZATION];
    params.page_num = @"1";
    params.page_size = @"5";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        [self.table.mj_header endRefreshing];

        ByNetNewsModel *model = result.data;
        [self.items removeAllObjects];
        [self.items addObjectsFromArray:model.results];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:0];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        
    } failure:^(ByNetBaseResult *result) {
        [self.table.mj_header endRefreshing];
    }];
}
/** 常务理事会 */
- (void)postQueryForOrganization_normal {
    
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_ORGANIZATION_NORMAL];
    params.page_num = @"1";
    params.page_size = @"5";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        
        ByNetNewsModel *model = result.data;
        [self.normaelItems removeAllObjects];
        [self.normaelItems addObjectsFromArray:model.results];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:1];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        
    } failure:^(ByNetBaseResult *result) {
    }];
}
/** 副理事会 */
- (void)postQueryForOrganization_vice {
    
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_ORGANIZATION_VICE];
    params.page_num = @"1";
    params.page_size = @"5";
    
    [ByNetNewsApi newsQuery:params success:^(ByNetBaseResult *result) {
        ByNetNewsModel *model = result.data;
        [self.viceItems removeAllObjects];
        [self.viceItems addObjectsFromArray:model.results];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:2];
        [self.table reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        
    } failure:^(ByNetBaseResult *result) {
    }];
}

#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64 - 40) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    /** 头视图 */
    UIView *view = [[UIView alloc] initWithFrame:self.carouselView.bounds];
    [view addSubview:self.carouselView];
    self.table.tableHeaderView = view;

    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self postQuery];
    }];
    self.table.mj_header = header;

}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1 + self.items.count;
    } else if (section == 1) {
        return 1 + self.normaelItems.count;
    } else if (section == 2) {
        return 1 + self.viceItems.count;
    }
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.font = FONT(14);
    }
    
//    /** 通知 */
//    if (indexPath.section == 0) {
//        ByNotificationCell *cell = [ByNotificationCell createCellInTableview:tableView];
//        return cell;
//    }
    
    NSArray *titles = @[@"理事长单位", @"常务理事长单位", @"副理事长单位"];
    /** row = 0 的单元格是标题单元格 */
    if (indexPath.row == 0) {
        ByTitleCell *cell = [ByTitleCell createCellInTableview:tableView];
        cell.titleLabel.text = titles[indexPath.section];
        return cell;
    } else {
        /** 理事长单位 */
        /** 常务理事长单位 */
        /** 副理事长单位 */
        ByContentCell *cell = [ByContentCell createCellInTableview:tableView cellType:ByContentCellTypeDefautl];
        if (indexPath.section == 0) {
            cell.model = self.items[indexPath.row - 1];
        }
        if (indexPath.section == 1) {
            cell.model = self.normaelItems[indexPath.row - 1];
        }
        if (indexPath.section == 2) {
            cell.model = self.viceItems[indexPath.row - 1];
        }

        return cell;
    }
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    /** 每组的第一个单元格都是高度40 */
    if (indexPath.row == 0) {
        return 40;
    } else {
        return 55;
    }
    return 40;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 0.5;
    } else {
        return 5;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    ByNetNewsItemModel *model = nil;
    if (indexPath.section == 0) {
        if (indexPath.row != 0) {
            model = self.items[indexPath.row - 1];
        } else {
            return;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row != 0) {
            model = self.normaelItems[indexPath.row - 1];
        } else {
            return;
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row != 0) {
            model = self.viceItems[indexPath.row - 1];
        } else {
            return;
        }
    }
    NSString *postID = model.id;
    NSString *urlStr = [NSString stringWithFormat:@"Webview/Post/viewPost?id=%@", postID];
    ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:urlStr];
    vc.navigationItem.title = model.post_cate_name;
    [self.navigationController pushViewController:vc animated:YES];

}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
